package cn.larry.tools.browser;

import cn.larry.tools.cons.AndroidConstants;
import cn.larry.tools.string.StringTool;

import java.util.Map;

/**
 * Larry
 * 2021/11/12 09:52
 * 浏览器工具类
 *
 * @Version 1.0
 */
public class BrowserTool {

    /**
     * 返回判断浏览器类型（参考'BrowserTypeEnum.java'）
     * 【P：判断浏览器类型，包含关键字符串的检验并不一定正确】
     *
     * @param userAgent
     * @return
     * @throws Exception
     */
    public static String judgeBrowserType(String userAgent) throws Exception {
        String browserType = "" ;
        for (Map.Entry<String, String[]> map : BrowserUserAgentEnum.BROWSERUSERAGENT_MAP.entrySet()) {
            String[] value = map.getValue();
            boolean flag = false;
            // 匹配所有子字符串都包含
            for (String pivotalSubUserAgent : value) {
                if (userAgent.indexOf(pivotalSubUserAgent) > -1)
                    flag = true;
                else{
                    flag = false;
                    break;
                }
            }
            if (flag) {
                browserType = map.getKey();
                break;
            }
        }
        if (StringTool.isBlank(browserType))
            throw new Exception("Unknown browser type" );
        return browserType;
    }

    /**
     * 随机生成（安卓）微信手机浏览器格式的UserAgent（警告：仅测试使用，使用过程中产生风险后果自负）
     *
     * @return
     */
    public static String getUserAgentByMobilePhoneWechatBrowser() {
        String appversion = getAppVersionWeixin();

        String userAgent = "Mozilla/5.0 (Linux; U; Android "
                + getAndroid()
                + " "
                + getPhoneModel()
                + "; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/"
                + getChrome()
                + " XWEB/2759 MMWEBSDK/201201 Mobile Safari/537.36 MMWEBID/586 MicroMessenger/"
                + appversion + "(0x2800003D) Process/toolsmp WeChat/arm64 "
                + "Weixin(nt:" + getDefinedNt() + "Language/zh_CN ABI/arm64" ;
        return userAgent;
    }

    /**
     * 随机生成（安卓）支付宝手机浏览器格式的UserAgent（警告：仅测试使用，使用过程中产生风险后果自负）
     *
     * @return
     */
    public static String getUserAgentByMobilePhoneAlipayBrowser() {
        String uws = getUWS();
        String appversion = getAppVersionAlipay();

        String userAgent = "Mozilla/5.0 (Linux; U; Android "
                + getAndroid()
                + "; zh-CN; "
                + getPhoneModel()
                + ") AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 "
                + "UWS/" + uws
                + " Mobile Safari/537.36 "
                + "UCBS/" + getUCBS(uws)
                + " NebulaSDK/1.8.100112 Nebula "
                + "AlipayDefined(nt:" + getDefinedNt() + ",ws:" + getDefinedWsAlipay() + getDefinedAcAlipay() + ") "
                + "AliApp(AP/" + appversion + ") "
                + "AlipayClient/" + appversion
                + " Language/zh-Hans useStatusBar/true isConcaveScreen/true Region/CN NebulaX/1.0.0 Ariver/1.0.0" ;
        return userAgent;
    }


    /**
     * 微信APP版本号
     */
    private static String getAppVersionWeixin() {
        return AndroidConstants.weixinAppVersion[getRadomMethodsToInt(0, AndroidConstants.weixinAppVersion.length - 1)];
    }

    /**
     * 安卓版本
     */
    private static String getAndroid() {
        return AndroidConstants.androidVersion[getRadomMethodsToInt(0, AndroidConstants.androidVersion.length - 1)];
    }

    /**
     * 手机型号
     */
    private static String getPhoneModel() {
        return AndroidConstants.phoneModel[getRadomMethodsToInt(0, AndroidConstants.phoneModel.length - 1)];
    }

    private static String getChrome() {
        String s1 = getRadomMethodsToString(60, 90);
        String s2 = getRadomMethodsToString(3100, 3999);
        String s3 = getRadomMethodsToString(1, 200);
        return s1 + ".0." + s2 + "." + s3;
    }

    /**
     * 网络
     */
    private static String getDefinedNt() {
        return AndroidConstants.network[getRadomMethodsToInt(0, AndroidConstants.network.length - 1)];
    }

    private static String getUCBS(String uws) {
        String s1 = getRadomMethodsToString(5, 9);
        String s2 = getRadomMethodsToString(1000, 9999);
        String s3 = getRadomMethodsToString(1000, 9999);
        return uws + "_200" + s1 + s2 + s3;
    }

    private static String getUWS() {
        String s1 = getRadomMethodsToString(1, 255);
        String s2 = getRadomMethodsToString(0, 255);
        String s3 = getRadomMethodsToString(0, 255);
        String s4 = getRadomMethodsToString(0, 255);
        return s1 + "." + s2 + "." + s3 + "." + s4;
    }

    /**
     * 支付宝APP版本号
     */
    private static String getAppVersionAlipay() {
        return AndroidConstants.alipayAppVersion[getRadomMethodsToInt(0, AndroidConstants.alipayAppVersion.length - 1)];
    }

    private static String getDefinedWsAlipay() {
        String s1 = getRadomMethodsToString(301, 399);
        String s2 = getRadomMethodsToString(0, 999);
        String s3 = getRadomMethodsToString(1, 9);
        return s1 + "|" + s2 + "|" + s3 + ".0" ;
    }

    private static String getDefinedAcAlipay() {
        String[] list_AlipayDefined_ac = {",ac:sp" , ""};
        return list_AlipayDefined_ac[getRadomMethodsToInt(0, list_AlipayDefined_ac.length - 1)];
    }

    /**
     * 获取某个区间范围的随机数
     *
     * @param min
     * @param max
     * @return
     */
    private static int getRadomMethodsToInt(int min, int max) {
        return ((int) (Math.random() * (max - min))) + min;
    }

    /**
     * 获取某个区间范围的随机数
     *
     * @param min
     * @param max
     * @return
     */
    private static String getRadomMethodsToString(int min, int max) {
        return getRadomMethodsToInt(min, max) + "" ;
    }

    /*public static void main(String[] args) throws Exception {
        while (true) {
            System.out.println(judgeBrowserType(getUserAgentByMobilePhoneWechatBrowser()));
            java.util.concurrent.TimeUnit.SECONDS.sleep(3);
        }
    }*/

}
